import * as common from './common.js';
import * as db from './db.js'
import {
	apiBaseUrl
} from './config.js'

// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'addComment',
	'addRelation',
	'getMovieById',
	'addPraise',
	'addFollow',
	'getFollowCommmentById',
	'getCommentById',
	'addComm',
	'userInfoInit',
	'getSeenMovie',
	'getWantMovie',
	'getUserCommentById',
	'addCollection'
];

const post = (url, data, callback) => {
	uni.showLoading({
		title: '加载中'
	});

	// 判断token是否存在
	if (methodsToken.indexOf(url) >= 0) {
		// 获取用户token
		let userToken = db.get("userToken");
		if (!userToken) {
			common.jumpToLogin();
			return false;
		} else {
			data.userToken = userToken;
		}
	} else {
		let userToken = db.get("userToken");
		if (userToken) {
			data.userToken = userToken;
		}
	}



	uni.request({
		url: apiBaseUrl + url,
		data: data,
		header: {
			'Accept': 'application/json',
			// 'Content-Type': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'POST',
		success: (response) => {
			uni.hideLoading();
			const result = response.data;
			callback(result);
		},
		complete: () => {
			uni.hideLoading();
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
	});

}

const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: apiBaseUrl + url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	});
}

/**
 * 所有的请求
 */
//用户登录
export const login = (data, callback) => post('login', data, callback);
//修改用户的初始的信息
export const upinfoLogin = (data, callback) => post('upinfoLogin', data, callback);
//更新用户界面显示的信息
export const userInfoInit = (data, callback) => post('userInfoInit', data, callback);
//获取用户信息
export const userInfoById = (data, callback) => post('userInfoById', data, callback);
//获取首页电影信息
export const getmovieListData = (callback) => get('getmovieListData', callback);
//获取不同类型的电影（分页处理）
export const getMovieByType = (data, callback) => post('getMovieByType', data, callback);
//根据id获取电影信息
export const getMovieById = (data, callback) => post('getMovieById', data, callback);
//发表评论
export const addComment = (data, callback) => post('addComment', data, callback)
//用户选择想看还是看过
export const addRelation = (data, callback) => post('addRelation', data, callback)
//用户点赞或者取消点赞
export const addPraise = (data, callback) => post('addPraise', data, callback)
//获取热门评论
export const getHotCommentById = (data, callback) => post('getHotCommentById', data, callback)
//获取最新评论
export const getNewCommentById = (data, callback) => post('getNewCommentById', data, callback)
//获得关注的评论
export const getFollowCommmentById = (data, callback) => post('getFollowCommmentById', data, callback)
//获取自己的评论
export const getUserCommentById = (data, callback) => post('getUserCommentById', data, callback)
//用户关注
export const addFollow = (data, callback) => post('addFollow', data, callback)
//获取指定评论信息
export const getCommentById = (data, callback) => post('getCommentById', data, callback)
//获取二级评论
export const getCommList = (data, callback) => post('getCommList', data, callback)
//添加评论
export const addComm = (data, callback) => post('addComm', data, callback)
//获取用户看过的电影
export const getSeenMovie = (data, callback) => post('getSeenMovie', data, callback)
//获取用户想看的电影
export const getWantMovie = (data, callback) => post('getWantMovie', data, callback)
//获取用户热搜
export const getHotList = (data, callback) => post('getHotList', data, callback);
//获取新闻
export const getNewsList = (data, callback) => post('getNewsList', data, callback);
//获取新闻详情
export const getNewsInfo = (data, callback) => post('getNewsInfo', data, callback);
//收藏
export const addCollection = (data, callback) => post('addCollection', data, callback);
export {
	apiBaseUrl
};
